use crate::{
    controller, dto,
    http::server::{
        error::Error, handler::result::IntoJsonResponseResult, middleware::ValidatedJson,
    },
};
use axum::{extract::State, Json};

use std::sync::Arc;

pub struct EndpointHandler {
    controller_set: Arc<controller::ControllerSet>,
}

impl EndpointHandler {
    pub fn new(
        controller_set: Arc<controller::ControllerSet>,
    ) -> Self {
        Self {
            controller_set,
        }
    }

    pub async fn add(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::AddRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::core::endpoint::AddResponseData>>, Error>
    {
        state
            .controller_set
            .endpoint
            .add(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn list(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::ListRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::endpoint::ListResponseData>>, Error>
    {
        state
            .controller_set
            .endpoint
            .list(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn list_info(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::ListRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::endpoint::ListInfoResponseData>>, Error>
    {
        state
            .controller_set
            .endpoint
            .list_info(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn get(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::GetRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::core::endpoint::EndpointItem>>, Error> {
        state
            .controller_set
            .endpoint
            .get(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn get_info(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::GetRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::endpoint::EndpointInfo>>, Error> {
        state
            .controller_set
            .endpoint
            .get_config(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn update(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::UpdateRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .endpoint
            .update(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn delete(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::DeleteRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .endpoint
            .delete(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn exists(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint::ExistsRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<wanmesh_common::dto::ExistsResponseData>>, Error>
    {
        state
            .controller_set
            .endpoint
            .exists(&request.0)
            .await
            .into_json_response_result()
    }
}
